/**
 * Created by Fakin on 2021-05-13
 */
const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')

function resolve (dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  publicPath: '/',
  outputDir: 'dist', // 打包输出文件夹名称
  assetsDir: 'static', // 打包输出静态资源文件夹名称
  lintOnSave: process.env.NODE_ENV === 'development', // lint校验
  productionSourceMap: false,
  devServer: {
    port: 8080,
    open: true,
    proxy: {
      '/api': {
        target: 'https://way.jd.com/jisuapi/',
        changeOrigin: true,
        pathRewrite: {
          '/api': ''
        }
      },
      '/toutiao': {
        target: 'https://www.toutiao.com/',
        changeOrigin: true,
        pathRewrite: {
          '/toutiao': ''
        }
      }
    }
  },
  css: {
    // loaderOptions: {
    //   sass: {
    //     data: `
    //       @import "@/styles/element-ui.scss";
    //     `
    //   }
    // }
  },
  configureWebpack: config => {
    config.performance = {
      hints: false
    }
    if (process.env.NODE_ENV === 'production') {
      // 将每个依赖包打包成单独的js文件
      // 配置删除 console.log和注视
      config.optimization = {
        minimizer: [
          new TerserPlugin({
            terserOptions: {
              ecma: undefined,
              warnings: false,
              parse: {},
              output: {
                comments: false
              },
              compress: {
                drop_console: true,
                drop_debugger: false,
                pure_funcs: ['console.log'] // 移除console
              }
            }
          })
        ]
      }
    }
  },
  chainWebpack: config => {
    config.plugin('html').tap(args => {
      args[0].title = '明日头条'
      return args
    })
    config.resolve.alias.set('@', resolve('src'))
    // 修复热更新失效
    config.resolve.symlinks(true)
    // 去除预加载
    config.plugins.delete('prefetch')
    config.plugins.delete('preload')
    // webpack打包提取公共代码
    config.optimization.splitChunks({
      chunks: 'all',
      cacheGroups: {
        libs: {
          name: 'chunk-libs',
          test: /[\\/]node_modules[\\/]/,
          priority: 10,
          chunks: 'initial'
        },
        elementUI: {
          name: 'chunk-elementUI',
          priority: 20,
          test: /[\\/]node_modules[\\/]_?element-ui(.*)/
        },
        commons: {
          name: 'chunk-commons',
          test: resolve('src/components'),
          minChunks: 3,
          priority: 5,
          reuseExistingChunk: true
        }
      }
    })
    // 打包压缩
    if (process.env.NODE_ENV === 'production') {
      // 开启js、css gzip压缩
      return {
        plugins: [new CompressionPlugin({
          test: /\.js$|\.html$|\.css/,
          threshold: 10240,
          deleteOriginalAssets: false
        })]
      }
    }
    // 图片压缩
    // config.module
    //   .rule('images')
    //   .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
    //   .use('image-webpack-loader')
    //   .loader('image-webpack-loader')
    //   .options({
    //     bypassOnDebug: true
    //   })
    //   .end()
  }

}
